/**
 * Created by yangchen on 2017-09-15.
 */
var invitation = new Vue({
    el:"#invitation",
    data:{
        userName:"",
        userImg:"",
        openId:"",
        QRcode:""
    },
    mounted:function () {
        this.getQRcode();
        this.getParams();
        this.shareConfig();
    },
    methods:{
        getParams: function () {
            API.getAuthInfo().then(function (data) {
                invitation.userName = data.nickName;
                invitation.openId = data.openId;

                var image = new Image();
                image.crossOrigin = '';
                image.src = data.headImgUrl;
                image.onload = function() {
                    var base64 = invitation.getBase64Image(image);
                    invitation.userImg = base64;

                    // 渲染成图片
                    var node =$('#invitation')[0];
                    html2canvas(node).then(function(canvas) {
                        var html_canvas = canvas.toDataURL();
                        var imgHtml = "<img width='100%' src='"+ html_canvas +"' />";
                        $("#img-container").html(imgHtml);
                        $("#invitation").hide();
                    });
                    // domtoimage.toPng(node)
                    //     .then(function (dataUrl) {
                    //         var img = new Image();
                    //         img.src = dataUrl;
                    //         $("#img-container").html(img);
                    //         $("#invitation").hide();
                    //     })
                    //     .catch(function (error) {
                    //         console.error('oops, something went wrong!', error);
                    //     });
                }

            })
        },
        getBase64Image: function(img) {
            var canvas = document.createElement("canvas");
            canvas.width = img.width;
            canvas.height = img.height;

            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width, img.height);
            var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
            var dataURL = canvas.toDataURL("image/"+ext);
            return dataURL;
        },
        getQRcode:function () {
            var index = location.href.lastIndexOf("/");
            var url = location.href.slice(0, index) + "/shareRecommend.html?openId=" + Util.getCookie("OPENID");
            Util.setQRCode("QRcode", url, 60, 60);
        },
        shareConfig: function() {
            API.getShareConfig({
                isWxAuth: false,
                currentUrl: location.href
            }).then(function (data) {
                wx.config({
                    debug: false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    signature: data.signature,
                    jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareQZone']
                })
                wx.ready(function () {

                    wx.onMenuShareAppMessage({
                        title: "寻找财税分享达人", // 分享标题
                        desc: '我正在参加“寻找财税分享达人”活动，有机会获得超级福利，快来帮我增加影响力吧！', // 分享描述
                        link: location.href, // 分享链接
                        imgUrl: './img/share_1.png' // 分享图标
                    });


                    wx.onMenuShareTimeline({
                        title: '真心推荐 | 我正在参加“寻找财税分享达人”活动，快来帮我增加影响力吧！',
                        link: location.href,
                        imgUrl: './img/share_1.png'
                    });

                });
                wx.error(function (res) {
                    console.log('wx.error: ' + JSON.stringify(res));
                });
            })
        }
    }
});